JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is commonly used for transmitting data in web applications.
JSON syntax is derived from JavaScript object notation syntax:
Here is an example of a JSON object:
{
"firstName": "John",
"lastName": "Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY"
},
"phoneNumbers": [
{ "type": "home", "number": "212-555-1234" },
{ "type": "office", "number": "646-555-4567" }
]
}
You can convert a JSON string into a JavaScript object using the JSON.parse()
method:
let jsonString = '{"firstName":"John","lastName":"Doe","age":30}';
let jsonObject = JSON.parse(jsonString);
console.log(jsonObject.firstName); // John
You can convert a JavaScript object into a JSON string using the JSON.stringify()
method:
let jsObject = { firstName: "John", lastName: "Doe", age: 30 };
let jsonString = JSON.stringify(jsObject);
console.log(jsonString); // {"firstName":"John","lastName":"Doe","age":30}
JSON is commonly used for:
For more detailed information, you can check out resources like W3Schools[^1^][1] and MDN Web Docs[^2^][3].